<?php
/**
 * @ClassName: Member
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-10-20 18:55
 * @Version: V1.0.0
 */

namespace app\test\controller;

use app\common\enum\OrderStatus;
use app\common\enum\OrderType;
use app\common\enum\PayStatus;
use app\common\enum\ShippingStatus;
use app\common\logic\CustomerLogic;
use app\common\model;
use think\Controller;

class Member extends Controller
{
    function index()
    {
        $member_id_list = model\Member::where('relation', 'like', '%,10001,%')->field('id')->select();
        $member_ids = [];
        foreach ($member_id_list as $item) {
            $member_ids[] = $item['id'];
        }

        $where = [];
        $where[] = ['o.member_id', 'in', $member_ids];
        $where[] = ['o.order_type', '=', OrderType::water];//订水订单
        $where[] = ['o.order_tag', '=', 'bucket_water'];//订单标识
        $where[] = ['o.pay_status', '=', PayStatus::pay];//已支付
        $where[] = ['o.shipping_status', '=', shippingStatus::shipped];//已发货
        $where[] = ['o.order_status', 'in', [OrderStatus::WAITCCOMMENT, OrderStatus::FINISH]];//已收货/已完成

        $num = model\ShopOrder::alias('o')->leftJoin('shop_order_goods og', 'og.order_id=o.id')->where($where)->count('og.total_num');
        $sql = model\ShopOrder::getLastSql();

        return json(['code' => 1, 'data' => $num, 'sql' => $sql]);
    }

    /**
     * 创建会员
     * @return \think\response\Json
     * @throws \think\Exception
     */
    function createMember()
    {
        $mobile = input('mobile');

        $member = model\Member::where('username', $mobile)->find();
        if ($member) {
            return json(['code' => 0, 'msg' => $mobile . '会员已经存在']);
        }

        $default_password = env('default_password');
        $arr = passCrypt($default_password);
        $data = [
            'username' => $mobile,
            'nickname' => env('user_name') . mt_rand(100, 999),
            'mobile' => $mobile,
            'email' => '',
            'password' => $arr[0],
            'salt' => $arr[1],
            'avatar' => '/static/images/avatar.png',
            'invite_type' => 0,
            'invite_store' => 0,
            'first_leader' => 0,
            'pay_password' => passEncryp($arr[1], '123456'),
            'super_pass' => passEncryp($arr[1]),
            'create_time' => date_now(),
        ];

        $member_id = model\Member::insertGetId($data);

        if (!$member_id) {
            return json(['code' => 0, 'msg' => $mobile . '会员创建失败']);
        }

        // 创建客户
        $res = (new CustomerLogic())->createCustomer($data['nickname'], $mobile, $member_id);

        if ($res['code'] == 1) {
            return json(['code' => 1, 'msg' => $mobile . '会员创建成功']);
        } else {
            return json($res);
        }
    }

    /**
     * 批量重置密码
     * @return \think\response\Json
     */
    function batchSetPassword()
    {
        $list = model\Member::select();
        $default_password = env('default_password');
        $count = 0;
        foreach ($list as &$member) {
            $arr = passCrypt($default_password);
            $member['password'] = $arr[0];
            $member['salt'] = $arr[1];
            $member['pay_password'] = passEncryp($arr[1], '123456');
            $member['super_pass'] = passEncryp($arr[1]);

            $res = $member->save();
            if ($res) {
                $count++;
            }
        }

        return json(['code' => 1, 'msg' => '共' . count($list) . '条数据，修改成功' . $count . '条']);
    }

}